package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.UserBanquetFeastReward;
import com.yanqu.road.entity.banquet.cross.CrossFeastPartyTempData;
import com.yanqu.road.entity.enums.eBanquetType;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_CROSS_PARTY_FEAST_SYNC,desc = "")
public class CrossFeastPartySyncCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        BanquetProto.CrossFeastPartySyncMsg syncMsg = BanquetProto.CrossFeastPartySyncMsg.parseFrom(packet.getBytes());
        List<BanquetProto.CrossFEastPartyTempMsg> msgList = syncMsg.getMsgList();
        if (msgList.size() > 0) {
            Map<String, CrossFeastPartyTempData> tempDataMap = new ConcurrentHashMap<>();
            for (BanquetProto.CrossFEastPartyTempMsg msg : msgList) {
                CrossFeastPartyTempData data = new CrossFeastPartyTempData(msg.getBanquetId(), msg.getOverTime(),msg.getUserCount());
                tempDataMap.put(data.getBanquetId(), data);
            }
            BanquetMgr.syncCrossFeastParty(tempDataMap);
            for (BanquetProto.CrossFEastPartyTempMsg msg : msgList) {
                String banquetId = msg.getBanquetId();
                noticeToAllOnlinePlayer(banquetId);
            }
        }
    }
    private void noticeToAllOnlinePlayer(String partyId) {
        try {
            CrossFeastPartyTempData data = BanquetMgr.getCrossFeastPartyTempDataMap().get(partyId);
            if (data == null) {
                return;
            }
            if (!BanquetConfigMgr.isSendBanquetFeastRewardPoint(data.getUserCount())) {
                return;
            }
            List<GamePlayer> allOnlinePlayer = GamePlayerMgr.getAllOnlinePlayer();
            for (GamePlayer gamePlayer : allOnlinePlayer) {
                Map<String, Map<Integer, UserBanquetFeastReward>> banquetRewardMap = gamePlayer.getModule(BanquetModule.class).getUserBanquetFeastRewardList();
                Map<Integer, UserBanquetFeastReward> rewardMap = banquetRewardMap.get(partyId);
                Map<String, Map<Integer, UserBanquetFeastReward>> tempMap = new HashMap<>();
                if (rewardMap == null) {
                    rewardMap = new HashMap<>();
                }
                tempMap.put(partyId, rewardMap);
                BanquetProto.BanquetPopRewardSync.Builder respMsg = BanquetPb.parseBanquetFeastRewardListResp(tempMap, eBanquetType.ServerFeastBanquet.getValue(), true);
                gamePlayer.sendPacket(Protocol.U_BANQUET_FEAST_REWARD_SYNC, respMsg);
            }
        } catch (Exception e) {

        }
    }
}
